package com.sgcc.epri.dcloud.conf.db;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.EnumOrdinalTypeHandler;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
//配置mapper路径
@MapperScan(basePackages = "com.sgcc.epri.dcloud.mapper.dm", sqlSessionFactoryRef = "dmSqlSessionFactory")
public class DMDataSourceConfig {

    @Autowired
    private Environment env;
    // 将这个对象放入Spring容器中
    @Bean(name = "dmDataSource")
    // 表示这个数据源是默认数据源
    @Primary
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.druid.dm")
    public DataSource getDMDateSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "dmSqlSessionFactory")
    // 表示这个数据源是默认数据源
    @Primary
    // @Qualifier表示查找Spring容器中名字为test1DataSource的对象
    public SqlSessionFactory dmSqlSessionFactory(@Qualifier("dmDataSource") DataSource datasource)
            throws Exception{
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        Properties props = new Properties();
        props.put("dm.private-schema-name",env.getProperty("dm.private-schema-name"));
        props.put("dm.public-schema-name",env.getProperty("dm.public-schema-name"));
        bean.setConfigurationProperties(props);
        bean.setObjectWrapperFactory(new MapWrapperFactory());
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper/dm/*.xml"));
        // mybatisplus 分页配置
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setDefaultEnumTypeHandler(EnumOrdinalTypeHandler.class);
        bean.setConfiguration(configuration);
//        sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations());
        bean.setPlugins(new Interceptor[]{new PaginationInterceptor()});

        return bean.getObject();
    }

    @Bean("dmSqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate test1SqlSessionTemplate(
            @Qualifier("dmSqlSessionFactory") SqlSessionFactory sessionFactory){
        return new SqlSessionTemplate(sessionFactory);
    }

}